package cn.edu.wfit.modules.infomanagement.service.impl;

import cn.edu.wfit.common.core.utils.DateUtils;
import cn.edu.wfit.modules.infomanagement.domain.TBus;
import cn.edu.wfit.modules.infomanagement.domain.TBusDate;
import cn.edu.wfit.modules.infomanagement.domain.TBusSchedule;
import cn.edu.wfit.modules.infomanagement.dto.ScheduleAndDate;
import cn.edu.wfit.modules.infomanagement.mapper.TBusDateMapper;
import cn.edu.wfit.modules.infomanagement.mapper.TBusMapper;
import cn.edu.wfit.modules.infomanagement.mapper.TBusScheduleMapper;
import cn.edu.wfit.modules.infomanagement.service.ITBusDateService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 日期预约Service业务层处理
 *
 * @author zhanghao
 * @date 2025-11-07
 */
@Service
public class TBusDateServiceImpl implements ITBusDateService {
    @Autowired
    private TBusDateMapper tBusDateMapper;
    @Autowired
    private TBusScheduleMapper tBusScheduleMapper;
    @Autowired
    private TBusMapper tBusMapper;

    /**
     * 查询日期预约
     *
     * @param id 日期预约主键
     * @return 日期预约
     */
    @Override
    public TBusDate selectTBusDateById(Long id) {
        return tBusDateMapper.selectTBusDateById(id);
    }

    /**
     * 查询日期预约列表
     *
     * @param tBusDate 日期预约
     * @return 日期预约
     */
    @Override
    public List<TBusDate> selectTBusDateList(TBusDate tBusDate) {
        return tBusDateMapper.selectTBusDateList(tBusDate);
    }

    /**
     * 新增日期预约
     *
     * @param tBusDate 日期预约
     * @return 结果
     */
    @Override
    public int insertTBusDate(TBusDate tBusDate) {
        tBusDate.setCreateTime(DateUtils.getNowDate());
        return tBusDateMapper.insertTBusDate(tBusDate);
    }

    /**
     * 修改日期预约
     *
     * @param tBusDate 日期预约
     * @return 结果
     */
    @Override
    public int updateTBusDate(TBusDate tBusDate) {
        tBusDate.setUpdateTime(DateUtils.getNowDate());
        return tBusDateMapper.updateTBusDate(tBusDate);
    }

    /**
     * 批量删除日期预约
     *
     * @param ids 需要删除的日期预约主键
     * @return 结果
     */
    @Override
    public int deleteTBusDateByIds(Long[] ids) {
        return tBusDateMapper.deleteTBusDateByIds(ids);
    }

    /**
     * 删除日期预约信息
     *
     * @param id 日期预约主键
     * @return 结果
     */
    @Override
    public int deleteTBusDateById(Long id) {
        return tBusDateMapper.deleteTBusDateById(id);
    }

    /**
     * 根据日期查询详细信息
     *
     * @param date 日期
     * @return 日期预约
     */
    @Override
    public List<ScheduleAndDate> selectTBusDateByDate(String date) {

        //创建新列表
        List<ScheduleAndDate> result = new java.util.ArrayList<>();

        List<TBusDate> list = tBusDateMapper.selectTBusDateByDate(date);
        for (int i = 0; i < list.size(); i++) {
            //创建dto
            ScheduleAndDate scheduleAndDate = new ScheduleAndDate();
            Long scheduleId = list.get(i).getScheduleId();
            //根据scheduleId查询发车排班表
            TBusSchedule tBusSchedule = tBusScheduleMapper.selectTBusScheduleById(scheduleId);
            Long busId = tBusSchedule.getBusId();
            //根据busId查询公交车信息
            TBus tBus = tBusMapper.selectTBusById(busId);
            BeanUtils.copyProperties(list.get(i), scheduleAndDate);
            //将公交车信息设置到dto中
            scheduleAndDate.settBus(tBus);
            //将排班表信息设置到dto中
            scheduleAndDate.settBusSchedule(tBusSchedule);
            //将日期预约信息设置到dto中
            result.add(scheduleAndDate);
        }

        return result;
    }

    /**
     * 定时开启新的日期预约
     */
    @Scheduled(cron = "0 0 0 * * ?", zone = "Asia/Shanghai")
    //@Scheduled(cron = "0 58 8 * * ?", zone = "Asia/Shanghai")
    public void add() {
        System.out.println("Service 定时任务触发：每日零点执行,开启新的日期预约，当前时间：" + LocalDateTime.now());
        //查询状态正常的排班表
        TBusSchedule tBusSchedule = new TBusSchedule();
        tBusSchedule.setStatus(1L);
        List<TBusSchedule> list = tBusScheduleMapper.selectTBusScheduleList(tBusSchedule);

        //遍历排班表,若有后天的排班,则自动向日期预约表中添加一条数据
        int day = LocalDate.now().plusDays(3).getDayOfWeek().getValue();
        //将day转换为字符传
        String dayStr = Integer.toString(day);

        for (int i = 0; i < list.size(); i++) {
            TBusSchedule schedule = list.get(i);
            //获取排班表中的发车周循环
            if (schedule.getWeek() != null) {
                String[] weekArray = schedule.getWeek().split(",");
                //判断是否包含dayStr
                if (Arrays.asList(weekArray).contains(dayStr)) {
                    //创建日期预约对象
                    TBusDate tBusDate = new TBusDate();
                    tBusDate.setScheduleId(schedule.getId());
                    //获取今天日期,然后添加两天后的日期
                    LocalDate departureDate = LocalDate.now().plusDays(2);
                    tBusDate.setDepartureData(DateUtils.parseDate(departureDate));
                    //获取荷载人数
                    Long busId = schedule.getBusId();
                    //根据busId查询公交车信息
                    TBus tBus = tBusMapper.selectTBusById(busId);
                    Long maxCapacit = tBus.getMaxCapacity();
                    tBusDate.setMaxCapacit(maxCapacit);
                    tBusDate.setStatus(1L);
                    //向日期预约表中添加数据
                    tBusDateMapper.insertTBusDate(tBusDate);
                }
            }
        }
    }

    /**
     * 定时将结束的日期预约状态改为3已结束
     *
     */
    @Scheduled(cron = "0 0 0 * * ?", zone = "Asia/Shanghai")
    public void updateStatus() {
        System.out.println("Service 定时任务触发：每日零点执行,更新已结束的日期预约状态为3已结束，当前时间：" + LocalDateTime.now());
        //查询状态为1正常的日期预约
        TBusDate tBusDate = new TBusDate();
        tBusDate.setStatus(1L);
        List<TBusDate> list = tBusDateMapper.selectTBusDateList(tBusDate);
        //遍历日期预约表,若有结束的日期预约,则将状态改为3已结束
        for (int i = 0; i < list.size(); i++) {
            TBusDate date = list.get(i);
            //获取日期预约中的发车日期
            Date departureData = date.getDepartureData();
            //获取今天日期
            LocalDate nowDate = LocalDate.now();
            //判断是否结束
            if (departureData.before(DateUtils.parseDate(nowDate))) {
                //将状态改为3已结束
                date.setStatus(3L);
                //更新日期预约
                tBusDateMapper.updateTBusDate(date);
            }
        }
    }
}